
#include "TUIBase.h"
#include "TUIInfo.h"
#include <stdlib.h>


const char* strUnit[4] =  {"     kPa","     PSI","     MPa","     Bar"};
const char* strONOFF[2] = {"     OFF","     ON"};
const char* strReady[2] = {"     TTL","  Switch"};
const char* strStart[3] = {"    Auto","  Manual","     TTL"};
const char* strStartSignal[2] = {"     TTL","  Switch"};
const char* strAfterRun[2] = {"   Reset","    None"};
const char* strRecvAlarm[3] = {"Turn off","   Reset","    Stop"};


USER_CONFIG_DATA m_TUIConfig;
USER_CONFIG_DATA m_oldTUIConfig;
MethodStatus     m_MethodStatus;
int TUI_configSave();
int TUI_configStep(unsigned char index);

int TUI_configStep(unsigned char index)
{
  MSG80_SEND msg = TUI_defMsg80();
  m_TUIConfig = UserCfg_Get();
  TUI_updateConfig(0);
  msg.addr = 0x03;
  msg.data[0] = 0;
  msg.data[1] = 0;
  m_TUIStatus.cfgStep = index;
  m_TUIStatus.nowPage = 1;
  switch(index)
  {
  case TUI_CFG_GENERAL:
    msg.data[1] = PAGE_CFG_GENERAL;
    break;
  case TUI_CFG_LAN:
    msg.data[1] = PAGE_CFG_LAN;
    break;
  case TUI_CFG_ACCOUNT:
    msg.data[1] = PAGE_CFG_ACCOUNT;
    break;
  case TUI_CFG_ABOUT:
    msg.data[1] = PAGE_CFG_ABOUT;
    break;
  case TUI_CFG_LOADM:
    msg.data[1] = PAGE_CFG_LOADM;
  default:
    break;
  }
  return TUI_sendMsg((unsigned char*)(&msg),7);
}
int TUI_configLANAddr(unsigned short Addr,unsigned char Value)
{
  if(Addr < TUI_CFG_LAN_NETMASK_VALUE) //ip
    m_TUIConfig.LanIP[(Addr-TUI_CFG_LAN_IP_SET)/4] = Value;
  else if(Addr < TUI_CFG_LAN_GATEWAY_VALUE) //netmask
    m_TUIConfig.LanNetmask[(Addr-TUI_CFG_LAN_NETMASK_SET)/4] = Value;
  else
    m_TUIConfig.LanGateway[(Addr-TUI_CFG_LAN_GATEWAY_SET)/4] = Value;
  
  return TUI_dispData(Addr-0x0010,Value,TUI_DATA_OTHER);
}
int TUI_updateLAN()
{
  int i=0;
  for(i=0;i<4;i++)
  {
    TUI_dispData(TUI_CFG_LAN_IP_VALUE + i*4,m_TUIConfig.LanIP[i],TUI_DATA_OTHER);
    TUI_dispData(TUI_CFG_LAN_NETMASK_VALUE + i*4,m_TUIConfig.LanNetmask[i],TUI_DATA_OTHER);
    TUI_dispData(TUI_CFG_LAN_GATEWAY_VALUE + i*4,m_TUIConfig.LanGateway[i],TUI_DATA_OTHER);
  }
  return 1;
}
int TUI_updateMethodStatus()
{
  int i=0;
  if(m_MethodStatus.MethodStep == TUI_LOAD_METHOD_NULL)
    return 0;

  if(m_MethodStatus.MethodStep == TUI_LOAD_METHOD_SET_ON)
  {
    for(i=0;i<TUI_CH_NUM;i++)
    {
      if(m_TUIConfig.MethodUsing.CHs[i].setON != m_TUIStatus.CHs[i].isON)
        TUI_setCHOn(i,m_TUIConfig.MethodUsing.CHs[i].setON);//send to SEPC
    }
    m_MethodStatus.MethodStep = TUI_LOAD_METHOD_WAIT_ON;
  }
  else if(m_MethodStatus.MethodStep == TUI_LOAD_METHOD_WAIT_ON)
  {
    for(i=0;i<TUI_CH_NUM;i++)
    {
      if(m_TUIConfig.MethodUsing.CHs[i].setON != m_TUIStatus.CHs[i].isON)
        break;
    }
    if(i == TUI_CH_NUM)
    {
      for(i=0;i<TUI_CH_NUM;i++)
      {
        TUI_setCH_Pressure(i,m_TUIConfig.MethodUsing.CHs[i].Value[0].value_set);
        m_TUIConfig.MethodUsing.CHs[i].Value[0].value_set = m_TUIStatus.CHs[i].Value[0].value_set;
        m_TUIStatus.CHs[i] = m_TUIConfig.MethodUsing.CHs[i];
      }
      m_MethodStatus.MethodStep = TUI_LOAD_METHOD_NULL;
    }
  }
  return 0;
}
int TUI_configAccount(unsigned short Addr,long data)
{
  if(Addr == TUI_CFG_ACCOUNT_PSWD1)
    m_TUIStatus.PasswordNew1 = data;
  else if(Addr == TUI_CFG_ACCOUNT_PSWD2)
    m_TUIStatus.PasswordNew2 = data;
  else if(Addr == TUI_CFG_ACCOUNT_DEBUG)
  {

  }
  return 1;
}
int TUI_updateConfig(int reUpdate)
{
  int i=0;
  if(m_TUIConfig.PresUnit != m_oldTUIConfig.PresUnit || reUpdate)
  {
    TUI_dispInfo(0x2110,strUnit[m_TUIConfig.PresUnit],strlen(strUnit[m_TUIConfig.PresUnit])+1);
  }

  if(m_TUIConfig.AutoDownload != m_oldTUIConfig.AutoDownload || reUpdate)
    TUI_dispInfo(0x2120,strONOFF[m_TUIConfig.AutoDownload],strlen(strONOFF[m_TUIConfig.AutoDownload])+1);

  if(m_TUIConfig.WaitTime != m_oldTUIConfig.WaitTime || reUpdate)
    TUI_dispData(0x2130,m_TUIConfig.WaitTime,TUI_DATA_OTHER);

  if(m_TUIConfig.StartRun != m_oldTUIConfig.StartRun || reUpdate)
    TUI_dispInfo(0x2140,strStart[m_TUIConfig.StartRun],strlen(strStart[m_TUIConfig.StartRun])+1);
  
  if(m_TUIConfig.AfterRun != m_oldTUIConfig.AfterRun || reUpdate)
    TUI_dispInfo(0x2150,strAfterRun[m_TUIConfig.AfterRun],strlen(strAfterRun[m_TUIConfig.AfterRun])+1);
  
  if(m_TUIConfig.RecvAlarm != m_oldTUIConfig.RecvAlarm || reUpdate)
    TUI_dispInfo(0x2160,strRecvAlarm[m_TUIConfig.RecvAlarm],strlen(strRecvAlarm[m_TUIConfig.RecvAlarm])+1);

  if(m_TUIConfig.AfterError != m_oldTUIConfig.AfterError || reUpdate)
    TUI_dispInfo(0x2170,strRecvAlarm[m_TUIConfig.AfterError],strlen(strRecvAlarm[m_TUIConfig.AfterError])+1);

  if(m_TUIConfig.LockPswd != m_oldTUIConfig.LockPswd || reUpdate)
    TUI_dispInfo(0x2180,strONOFF[m_TUIConfig.LockPswd],strlen(strONOFF[m_TUIConfig.LockPswd])+1);

  if(m_TUIConfig.TimeHour != m_oldTUIConfig.TimeHour || reUpdate)
    TUI_dispData(0x2190,m_TUIConfig.TimeHour,TUI_DATA_OTHER);

  if(m_TUIConfig.TimeMinute != m_oldTUIConfig.TimeMinute || reUpdate)
    TUI_dispData(0x2198,m_TUIConfig.TimeMinute,TUI_DATA_OTHER);

  if(m_TUIConfig.DateYear != m_oldTUIConfig.DateYear || reUpdate)
    TUI_dispData(0x21A0,m_TUIConfig.DateYear,TUI_DATA_OTHER);

  if(m_TUIConfig.DateDay != m_oldTUIConfig.DateDay || reUpdate)
    TUI_dispData(0x21B0,m_TUIConfig.DateDay,TUI_DATA_OTHER);

  if(m_TUIConfig.DateMonth != m_oldTUIConfig.DateMonth || reUpdate)
    TUI_dispData(0x21B8,m_TUIConfig.DateMonth,TUI_DATA_OTHER);

  if(m_TUIConfig.LanType != m_oldTUIConfig.LanType || reUpdate)
  {
    TUI_dispIcon(TUI_CFG_LAN_DHCP,m_TUIConfig.LanType == 1);
    TUI_dispIcon(TUI_CFG_LAN_MANUAL,m_TUIConfig.LanType != 1);
  }

  if(reUpdate)
  {
    TUI_updateLAN();
  }

  m_oldTUIConfig = m_TUIConfig;
  return 1;
}
int TUI_changeGeneral(unsigned short Addr,long data)
{
  int reUpdate = 0;
  switch(Addr)
  {
  case TUI_BTN_OPT:
    reUpdate = 1;
    break;
  case TUI_CFG_GENERAL_UNIT:
    m_TUIConfig.PresUnit = m_TUIConfig.PresUnit<3? m_TUIConfig.PresUnit + 1 : 0;
    break;
  case TUI_CFG_GENERAL_AUTOD:
    m_TUIConfig.AutoDownload = m_TUIConfig.AutoDownload<1? m_TUIConfig.AutoDownload + 1 : 0;
    break;
  case TUI_CFG_GENERAL_WAITT:
    m_TUIConfig.WaitTime = data;
    break;
  case TUI_CFG_GENERAL_RUN:
    m_TUIConfig.StartRun = m_TUIConfig.StartRun<2? m_TUIConfig.StartRun + 1 : 0;
    break;
  case TUI_CFG_GENERAL_AFTERRUN:
    m_TUIConfig.AfterRun = m_TUIConfig.AfterRun<1? m_TUIConfig.AfterRun + 1 : 0;
    break;
  case TUI_CFG_GENERAL_RECVALARM:
    m_TUIConfig.RecvAlarm = m_TUIConfig.RecvAlarm<2? m_TUIConfig.RecvAlarm + 1 : 0;
    break;
  case TUI_CFG_GENERAL_AFTERERROR:
    m_TUIConfig.AfterError = m_TUIConfig.AfterError<2? m_TUIConfig.AfterError + 1 : 0;
    break;
  case TUI_CFG_GENERAL_LOCKPSWD:
    m_TUIConfig.LockPswd = m_TUIConfig.LockPswd<1? m_TUIConfig.LockPswd + 1 : 0;
    break;
  case TUI_CFG_GENERAL_HOUR:
    m_TUIConfig.TimeHour = data;
    break;
  case TUI_CFG_GENERAL_MINUTE:
    m_TUIConfig.TimeMinute = data;
    break;
  case TUI_CFG_GENERAL_YEAR:
    m_TUIConfig.DateYear = data;
    break;
  case TUI_CFG_GENERAL_MONTH:
    m_TUIConfig.DateMonth = data;
    break;
  case TUI_CFG_GENERAL_DAY:
    m_TUIConfig.DateDay = data;
    break;
  default:
    break;
  }
  return TUI_updateConfig(reUpdate);
}
int TUI_back2LoadM()
{
  MSG80_SEND msg = TUI_defMsg80();
  msg.addr = 0x03;
  msg.data[0] = 0;
  if(m_TUIStatus.nowPage == 1)
    msg.data[1] = PAGE_CFG_LOADM;
  else if(m_TUIStatus.nowPage == 2)
    msg.data[1] = PAGE_CFG_LOADM_2;
  return TUI_sendMsg((unsigned char*)(&msg),7);
}
int TUI_methodPreview(unsigned char method, unsigned char channel)
{
  MSG80_SEND msg = TUI_defMsg80();
  TUI_dispPreview(m_TUIConfig.Method[method].CHs[channel]);
  msg.addr = 0x03;
  msg.data[0] = 0;
  msg.data[1] = PAGE_CH_PREVIEW;
  return TUI_sendMsg((unsigned char*)(&msg),7);

}

int TUI_configLoadM(unsigned short Addr)
{
  int i = 0;
  unsigned short Offset = Addr - TUI_CFG_LOADM_ADDR_START;
  unsigned short Item = Offset/0x10 - 1;
  if(Offset%0x10 == 4) // save Method
  {
    for(i=0;i<6;i++)
      m_TUIConfig.Method[Item].CHs[i] = m_TUIStatus.CHs[i];
    TUI_configSave();
  }
  else if(Offset%0x10 == 8) // load Method
  {
//    m_MethodStatus.MethodSelected = Item;
//    m_MethodStatus.MethodStep = TUI_LOAD_METHOD_SET_ON;
    m_TUIConfig.MethodUsing = m_TUIConfig.Method[Item];
    TUI_configSave();
    for(i=0;i<TUI_CH_NUM;i++)
    {
      m_TUIStatus.CHs[i] = m_TUIConfig.MethodUsing.CHs[i];
    }
  }
  else //preview
  {
    m_TUIStatus.MethodPreview = Item;
    TUI_methodPreview(Item,0);
  }
  return 1;
}
int TUI_configVerify()
{
  if(m_TUIStatus.cfgStep ==TUI_CFG_ACCOUNT)
  {
    if(m_TUIStatus.PasswordNew1 == m_TUIStatus.PasswordNew2)
    {
      m_TUIConfig.Password = m_TUIStatus.PasswordNew1;
      return 0;
    }
    else
    {
      TUI_addInfo(ERROR_PASSWORD,ERROR_ONCE);
      return 1;
    }
  }
  return 0;
}
int TUI_configExec()
{
  char buff[100];
  if(m_TUIStatus.cfgStep == TUI_CFG_LAN)
  {    
    return UserCfg_Exec(CONFIG_EXEC_IP);
  }
  else if(m_TUIStatus.cfgStep == TUI_CFG_GENERAL)
  {
    TUI_dispIconUnit(m_TUIConfig.PresUnit);
  }
  return 0;
}
int TUI_configAfterSave()
{
  m_TUIStatus.cfgStep = TUI_CFG_NONE;
  TUI_dispHome();
  TUI_dispPage(PAGE_HOME);
  return 0;
}
int TUI_configSave()
{
  if(TUI_configVerify() != 0)
    return 1;
  if(UserCfg_Set(m_TUIConfig) != 0)
    return 1;
  if(UserCfg_Save() !=0)
    return 1;
  if(TUI_configExec() != 0)
    return 1;
mLog("save successfully!!\n");
  return TUI_configAfterSave();
}

int TUI_configRead()
{
  int length;
  FILE* fd;
  fd = fopen("TUIconfig.ini","rb+");
  length = fread(&m_TUIConfig,sizeof(m_TUIConfig),1,fd);
  fclose(fd);
  return 0;
}
